package org.dairy.farms.dao.hibernate;

import org.dairy.farms.dao.PersonDAO;
import org.dairy.farms.dao.generic.AbstractDAOImpl;
import org.dairy.farms.model.Livestock;
import org.dairy.farms.model.Person;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: gduggira
 * Date: Jun 13, 2011
 * Time: 3:26:15 PM
 */
@Repository(value="personDao")
public class PersonDAOImpl extends AbstractDAOImpl<Person, Long> implements PersonDAO {
    @Override
    public List<Person> findByRoleType(String[] roleTypeName, Long farmId) {
        Criteria criteria = basePersistence.getSession().createCriteria(Person.class);
        criteria.createCriteria("farm").add(Restrictions.eq("id",farmId));
        criteria.createAlias("roles","roles").add(Restrictions.in("roles.name",roleTypeName));
        return criteria.list();
    }

    @Override
    public List<Person> findByProperty(String propertyName, String propertyValue, int page, int start, int limit) {
         Criteria criteria = basePersistence.getSession().createCriteria(Person.class);
        criteria.add(Restrictions.like(propertyName, propertyValue)).setFirstResult(start).setMaxResults(limit);
        return criteria.list();
    }
}
